package es.udc.fic.hamNeggs.dao;

import es.udc.fic.hamNeggs.manager.ProjectManager;
import es.udc.fic.hamNeggs.manager.SprintManager;
import es.udc.fic.hamNeggs.manager.UserManager;
import es.udc.fic.hamNeggs.manager.UserStoriesCategoryManager;
import es.udc.fic.hamNeggs.manager.UserStoriesCommentManager;
import es.udc.fic.hamNeggs.pojo.UserStories;
import es.udc.fic.hamNeggs.pojo.UserStories.State;
import es.udc.fic.hamNeggs.pojo.UserStoriesComment;

import java.util.ArrayList;

public class InputUserStory {

    private String                  id;

    private String                  name;

    private String                  description;

    private String                  state;

    private String                  realstoryPoints;

    private String                  estimatedstoryPoints;

    private String                  user;

    private String                  sprint;

    private String                  project;

    private String                  category;

    private String                  stringCategory;

    private ArrayList<String>       comments       = new ArrayList<String>();

    private final ArrayList<String> stringComments = new ArrayList<String>();

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getRealstoryPoints() {
        return realstoryPoints;
    }

    public void setRealstoryPoints(String realstoryPoints) {
        this.realstoryPoints = realstoryPoints;
    }

    public String getEstimatedstoryPoints() {
        return estimatedstoryPoints;
    }

    public void setEstimatedstoryPoints(String estimatedstoryPoints) {
        this.estimatedstoryPoints = estimatedstoryPoints;
    }

    public void setNewComment(String comment) {
        comments.add(comment);
    }

    public ArrayList<String> getComments() {
        return comments;
    }

    public void setComments(ArrayList<String> comments) {
        this.comments = comments;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getSprint() {
        return sprint;
    }

    public void setSprint(String sprint) {
        this.sprint = sprint;
    }

    public String getProject() {
        return project;
    }

    public void setProject(String project) {
        this.project = project;
    }

    public ArrayList<String> getStringComments() {
        return stringComments;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getStringCategory() {
        return stringCategory;
    }

    public void setStringCategory(String stringCategory) {
        this.stringCategory = stringCategory;
    }

    public UserStories toUserStory() {
        UserStories ust = new UserStories();
        UserStoriesCommentManager cmtmng = new UserStoriesCommentManager();
        UserManager usrmng = new UserManager();
        ProjectManager prjmng = new ProjectManager();
        SprintManager sprmng = new SprintManager();
        UserStoriesCategoryManager ctgmng = new UserStoriesCategoryManager();

        if (id != null && id != "")
            ust.setId(new Long(id));

        if (name != null && name != "")
            ust.setName(name);

        if (description != null && description != "")
            ust.setDescription(description);

        if (state != null && state != "")
            ust.setState(State.valueOf(state));

        if (user != null && user != "")
            ust.setUser(usrmng.getUser(new Long(user)));

        if (sprint != null && sprint != "")
            ust.setSprint(sprmng.getSprint(new Long(sprint)));

        if (project != null && project != "")
            ust.setProject(prjmng.getProject(new Long(project)));

        if (category != null && category != "")
            ust.setUserStoriesCategory(ctgmng.getUserStoriesCategory(new Long(category)));

        if (realstoryPoints != null && !realstoryPoints.equalsIgnoreCase(""))
            ust.setRealstoryPoints(new Integer(realstoryPoints));

        if (estimatedstoryPoints != null && !estimatedstoryPoints.equalsIgnoreCase(""))
            ust.setEstimatedstoryPoints(new Integer(estimatedstoryPoints));

        if (comments != null && !comments.isEmpty())
            for (String cmt : comments)
                ust.setOneComment(cmtmng.getUserStoriesComment(new Long(cmt)));

        prjmng.close();
        sprmng.close();
        cmtmng.close();
        ctgmng.close();
        usrmng.close();

        return ust;

    }

    public void setUserStory(UserStories ust) {
        if (ust.getId() != null)
            id = ust.getId().toString();
        if (ust.getName() != null)
            name = ust.getName();
        if (ust.getDescription() != null)
            description = ust.getDescription();
        if (ust.getState() != null)
            state = ust.getState().getString();
        if (ust.getUser() != null)
            user = ust.getUser().getId().toString();
        if (ust.getSprint() != null)
            sprint = ust.getSprint().getId().toString();
        if (ust.getProject() != null)
            project = ust.getProject().getId().toString();
        if (ust.getUserStoriesCategory() != null)
            category = ust.getUserStoriesCategory().getId().toString();
        if (ust.getUserStoriesCategory() != null)
            stringCategory = ust.getUserStoriesCategory().getCategory();
        if (ust.getRealstoryPoints() != null)
            realstoryPoints = ust.getRealstoryPoints().toString();
        if (ust.getEstimatedstoryPoints() != null)
            estimatedstoryPoints = ust.getEstimatedstoryPoints().toString();
        if (ust.getComments() != null && !ust.getComments().isEmpty())
            for (UserStoriesComment ustcmt : ust.getComments()) {
                comments.add(ustcmt.getId().toString());
                stringComments.add(ustcmt.getComment());
            }
    }
}
